package com.example.module_home.Adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.graphics.Color;
import android.support.annotation.DrawableRes;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.example.module_home.R;
import com.example.module_home.databinding.HomeItemTreeviewBinding;
import com.unnamed.b.atv.model.TreeNode;

/**
 * Created by Administrator on 2018/5/10 0010.
 */

public class CodeTreeItemHolder extends TreeNode.BaseNodeViewHolder<CodeTreeItemHolder.IconTreeItem> {

    private HomeItemTreeviewBinding mBinding;
    private IconTreeItem mValue;

    public CodeTreeItemHolder(Context context) {
        super(context);
    }

    @Override
    public View createNodeView(TreeNode node, IconTreeItem value) {
        this.mValue = value;
        final LayoutInflater inflater = LayoutInflater.from(context);
        if(mBinding==null){
            mBinding = DataBindingUtil.inflate(inflater, R.layout.home_item_treeview, null, false);
        }
        mBinding.nodeValue.setText(value.title);
        mBinding.icon.setImageResource(value.icon);
        if(value.isTitle){
            mBinding.arrowIcon.setVisibility(View.GONE);
        }
        return mBinding.getRoot();
    }

    /**
     * 子项切换
     * @param active 是否是展开的
     */
    @Override
    public void toggle(boolean active) {
        if(!mValue.isTitle){
            mBinding.arrowIcon.setImageResource(active?R.drawable.ic_triangle_open:R.drawable.ic_triangle_close);
        }
    }

    public static class IconTreeItem {
        public @DrawableRes int icon;
        public String title;
        public boolean isTitle;
        public int id;
        public IconTreeItem(@DrawableRes int icon, String title,int id ,boolean isTitle) {
            this.icon = icon;
            this.title = title;
            this.isTitle = isTitle;
            this.id = id;
        }
    }
}
